//
//  Team9disscussCollectionViewCell.m
//  Team9Project
//
//  Created by dlios on 15/10/15.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9disscussCollectionViewCell.h"
#import "Team9discussTableViewCell.h"
#import "Team9MainDisscussModel.h"
#import <UIImageView+WebCache.h>
@implementation Team9disscussCollectionViewCell


-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        [self creatTableView];
        self.empty = [Team9EmptyTool toolWithScrollView:self.discussTableView];
    }
    return self;
}

-(void)creatTableView
{
    self.discussTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, self.contentView.frame.size.height) style:UITableViewStylePlain];
    self.discussTableView.delegate = self;
    self.discussTableView.dataSource = self;
    [self.contentView addSubview:self.discussTableView];
    
    self.discussTableView.rowHeight= 130 /667.0 * HEGHT;
    

    self.discussTableView.userInteractionEnabled = YES;
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    self.discussTableView.tableFooterView = view;
    
}
- (void)changecolor{
    [super changecolor];
    if ([Team9EveningType shareEvening].eveningFailling) {
        self.discussTableView.backgroundColor = [UIColor blackColor];
    }else{
        self.discussTableView.backgroundColor = [UIColor whiteColor];
    }
}

- (void)setAllDataArray:(NSMutableArray *)allDataArray{
    if (_allDataArray != allDataArray) {
        _allDataArray = allDataArray;
    }

    [self.discussTableView reloadData];
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *reuse = @"reuse";
    Team9discussTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[Team9discussTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
    }
    
    Team9MainDisscussModel *disscussModel = self.allDataArray[indexPath.row];


    
    
    if ([disscussModel.title isKindOfClass:[NSString class]]) {
        
         cell.titleLabel.text = disscussModel.title;
        
    }
    
    
    cell.discussLabel.text = disscussModel.reply_count;
    cell.seeLabel.text = disscussModel.view_count;
    cell.describeLabel.text = disscussModel.reply_info[@"content"];
    
    // 图片
    if (disscussModel.pics.count) {
        [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:disscussModel.pics[0][@"pic"]] placeholderImage:[UIImage imageNamed:@"111"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [UIView animateWithDuration:0.3 animations:^{
                CATransition *transition = [CATransition animation];
                transition.type = @"fade";
                [transition setDuration:0.5];
                [transition setRepeatCount:1];
                // 向imageView上添加动画效果
                [cell.picImageView.layer addAnimation:transition forKey:@"transition"];
            }];
        }];

    }
   
    // 头像
    if (disscussModel.pics.count) {
        [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:disscussModel.pics[0][@"pic"]] placeholderImage:[UIImage imageNamed:@"333"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            [UIView animateWithDuration:0.3 animations:^{
                CATransition *transition = [CATransition animation];
                transition.type = @"fade";
                [transition setDuration:0.5];
                [transition setRepeatCount:1];
                // 向imageView上添加动画效果
                [cell.headImageView.layer addAnimation:transition forKey:@"transition"];
            }];
        }];
        
    }
    
    [Team9Tool imageView:cell.headImageView type:disscussModel.author_type Id:disscussModel.author_id viewController:self.vc baiHiden:YES];
    
    cell.discussImageView.image = [UIImage imageNamed:@"iconfont-message"];
    cell.seeImageView.image = [UIImage imageNamed:@"iconfont-chakan"];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    Team9MainDisscussModel *model = self.allDataArray[indexPath.row];
    //把indexPath.row传回discussController
    self.block(indexPath.row, model.topic_id,model.author_nick);
    
}
@end
